package org.learn.dormi330;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

/**
 * DateTimeFormat is thread-safe and immutable.
 *
 * @author : dormi330
 * @since : 22/02/06
 */
public class DateTimeFormatterDemo {
    // 1. Create DateTimeFormatter
    private static DateTimeFormatter format_no_zone = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    private static DateTimeFormatter format_has_zone = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z");

    public static void main(String[] args) {
        localDateTimeTest();
        localDateTimeUTCTest();
        zonedDateTimeTest();
        /*
============ LocalDateTime ==============
2022-02-06 15:32:25
2022-02-06T15:32:25
============ LocalDateTimeUTC ==============
2022-02-06 07:32:25
2022-02-06T07:32:25
============ zonedDateTimeTest ==============
设置市区 +8
2022-02-06 15:32:25 +08:00
2022-02-06T15:32:25+08:00
        * */
    }

    private static void localDateTimeTest() {
        System.out.println("============ LocalDateTime ============== ");
        LocalDateTime dateTime = LocalDateTime.now();
        // LocalDateTime.now(ZoneOffset.UTC);
        String text = dateTime.format(format_no_zone);
        System.out.println(text); // 2022-02-06 15:26:08 CST

        LocalDateTime dt2 = LocalDateTime.parse(text, format_no_zone);
        System.out.println(dt2); // 2022-02-06T15:19:44
    }

    private static void localDateTimeUTCTest() {
        System.out.println("============ LocalDateTimeUTC ============== ");
//        LocalDateTime dateTime = LocalDateTime.now(ZoneOffset.UTC);
        LocalDateTime dateTime = LocalDateTime.now(ZoneOffset.UTC);
        String text = dateTime.format(format_no_zone);
        System.out.println(text); // 2022-02-06 15:26:08 CST

        LocalDateTime dt2 = LocalDateTime.parse(text, format_no_zone);
        System.out.println(dt2); // 2022-02-06T15:19:44
    }

    private static void zonedDateTimeTest() {
        System.out.println("============ zonedDateTimeTest ============== ");
        System.out.println("设置市区 +8");
        ZonedDateTime now = ZonedDateTime.now(ZoneOffset.ofHours(8));
        String text = now.format(format_has_zone);
        System.out.println(text);

        ZonedDateTime dt2 = ZonedDateTime.parse(text, format_has_zone);
        System.out.println(dt2); // 2022-02-06T15:26:08-06:00[America/Chicago]
    }
}
